// pages/cart/cart.js
import store from '../../store/index.js'
import { changecountadd, changecountreduce, deleteitem } from '../../store/actions/cartcountchange.js'
const app = getApp()
console.log(app)
Page({
  allseleted(){
    if(this.data.totalselected){
      store.getState().cart.cart.map(items => {
        if (items.isSelected) {
          items.isSelected = false
        }
        return items
      })
      console.log("yej")
      this.setData({
        cartItems: store.getState().cart.cart,
      })
      console.log(store.getState().cart.cart)
      this.totalselected()
      this.getselectedprice()
    }else{
      store.getState().cart.cart.map(items => {
        if (!items.isSelected) {
          items.isSelected = true
        }
        return items
      })
      console.log("yeh")
      this.setData({
        cartItems: store.getState().cart.cart,
      })
      this.totalselected()
      console.log(this.data.cartItems)
      this.getselectedprice()
    }
  },
  totalselected () {
    const seleteditem = store.getState().cart.cart.filter(items => {
      return items.isSelected === true
    }).length
    if (store.getState().cart.cart.length === seleteditem){
      this.setData({
        totalselected:true
      })
    }
    else{
      this.setData({
        totalselected: false
      })
    }
    if (store.getState().cart.cart.length === 0) {
      this.setData({
        totalselected: false
      })
    }
  },
  getselectedprice () {
    const text = store.getState().cart.cart.reduce((result, item) => {
      if(item.isSelected){
        result += item.count * item.price
      }
      return result
    }, 0);
    this.setData({
      selectedprice: text
    })
  },
  bindCheckbox (e) {
    store.getState().cart.cart.map(items => {
      if(items.id === e.currentTarget.dataset.itemid){
        if(items.isSelected) {
          items.isSelected = false
        }else{
          items.isSelected = true
        }
      }
      return items
    })
    console.log(this.data.cartItems)
    this.setData({
      cartItems: store.getState().cart.cart,
    })
    this.getselectedprice()
    this.totalselected()
  },
  deleteitem (e) {
    console.log(e)
    store.dispatch(deleteitem(e.currentTarget.dataset.itemid))
    wx.setStorageSync('fuckxxx', store.getState().cart.cart)
    this.totalselected()
    console.log(store.getState().cart.cart.length)
    app.setCartcount()
  },
  setdata(){
    this.setData({
      cartItems: store.getState().cart.cart,
    })
  },
  add (e) {
    store.dispatch(changecountadd(e.currentTarget.dataset.itemid))
    this.setData({
      cartItems: store.getState().cart.cart,
    })
    console.log(store.getState().cart.cart)
    wx.setStorageSync('fuckxxx', store.getState().cart.cart )
  },
  reduce(e) {
    store.dispatch(changecountreduce(e.currentTarget.dataset.itemid))
    this.setData({
      cartItems: store.getState().cart.cart,
    })
    wx.setStorageSync('fuckxxx', store.getState().cart.cart)
    
  },
  /**
   * 页面的初始数据
   */
  data: {
    cartItems:null,
    selectedprice:null,
    totalselected:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setdata()
    store.subscribe(this.setdata)
    console.log(store.getState().cart.cart)
    this.getselectedprice()
    store.subscribe(this.getselectedprice)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.setCartcount()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})